package tianrun.ziguan.api.calculate.config.service.common.edit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;
import tianrun.ziguan.api.calculate.config.dto.request.common.DeleteBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.SaveBody;
import tianrun.ziguan.api.calculate.config.entity.TIndicatorsModule;
import tianrun.ziguan.api.calculate.config.service.common.edit.AbstractDealService;
import tianrun.ziguan.api.calculate.config.service.ge.TGeTableConfigService;
import tianrun.ziguan.api.calculate.config.service.template.module.ITIndicatorsModuleService;
import tianrun.ziguan.api.calculate.config.utils.MapUtil;
import tianrun.ziguan.api.calculate.config.utils.StringUtil;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class IndicatorModuleDealService extends AbstractDealService {

    private final ITIndicatorsModuleService indicatorsModuleService;

    public IndicatorModuleDealService(TGeTableConfigService tableConfigService,
                                      ITIndicatorsModuleService indicatorsModuleService) {
        super(tableConfigService);
        this.indicatorsModuleService = indicatorsModuleService;
    }

    @Override
    public void before(SaveBody body) {
        String name = MapUtil.getString(body.getData(), "name");
        if (StringUtil.isEmpty(name))return;
        String templateId = MapUtil.getString(body.getData(), "template_id");
        LambdaQueryWrapper<TIndicatorsModule> wrapper = Wrappers.<TIndicatorsModule>lambdaQuery()
                .eq(TIndicatorsModule::getName, name)
                .eq(TIndicatorsModule::getTemplateId,templateId);
        if (Objects.equals(body.getOption(), SaveBody.Option.EDIT)) {
            wrapper.ne(TIndicatorsModule::getId, body.getId());
        }
        int count = indicatorsModuleService.count(wrapper);
        // todo
        Assert.isTrue(count == 0, name + "已存在！");
    }


    @Override
    public String tableName() {
        return "t_indicators_module";
    }

    @Override
    public void after(SaveBody body) {

    }

    @Override
    public void delBefore(DeleteBody body) {

    }

    @Override
    public void listAfter(List<Map<String, Object>> result) {

    }

    @Override
    public void delAfter(DeleteBody body) {

    }

    @Override
    public void infoAfter(Map<String, Object> bean) {

    }


}
